package invest.data.getdata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import invest.data.Data;
import invest.pojo.datapojo.YejiYuBao;
import invest.utils.DateUtil;
import invest.utils.HttpRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//获取现在和上个季度的业绩预报利润
public class YejiyubaoData implements Data<List<YejiYuBao>> {
    private final String pre = "http://datacenter.eastmoney.com/securities/api/data/v1/get?sortColumns=NOTICE_DATE%2CSECURITY_CODE&sortTypes=-1%2C-1&reportName=RPT_PUBLIC_OP_NEWPREDICT&columns=ALL&filter=(PREDICT_FINANCE_CODE%3D%22005%22)(REPORT_DATE%3D%27";
    private final String after = "%27))&pageSize=2000&pageNumber=";
    @Override
    public List<YejiYuBao> getData() throws IOException {
        List<YejiYuBao> yejiYuBaos = new ArrayList<>();
        Date now = new Date();
        Date lastSeason = DateUtil.getPastMonth(now, 3);
        String nowParam = getDateParam(now);
        String lastSeasonParm=getDateParam(lastSeason);
        if (nowParam.equals(lastSeasonParm)) {
            addYubaoData(yejiYuBaos, nowParam);
        } else {
            addYubaoData(yejiYuBaos, nowParam);
            addYubaoData(yejiYuBaos, lastSeasonParm);
        }
        return yejiYuBaos;
    }

    private String getDateParam(Date date) {
        int year = date.getYear()+1900;
        int month = date.getMonth()+1;
        int day = date.getDate();
        String stringMonth = null;
        if (month <= 3) {
            month = 3;
            stringMonth = "03";
            day = 31;
        } else if (month <= 6) {
            month = 6;
            stringMonth = "06";
            day = 30;
        } else if (month <= 9) {
            month = 9;
            stringMonth = "09";
            day = 30;
        } else {
            month = 12;
            stringMonth = "12";
            day = 31;
        }
        String dateParam = year + "-" + stringMonth + "-"+day;
        return dateParam;
    }
    private void addYubaoData(List<YejiYuBao> yejiYuBaos,String dateParam) {
        String url = pre + dateParam + after+"1";
        String result = HttpRequest.sendGet(url, null);
        JSONObject jb = JSONObject.parseObject(result);
        Integer counts=jb.getJSONObject("result").getInteger("count");
        JSONArray data;
        int times = 0;
        if (counts % 50 == 0) {
            times = counts / 50;
        } else {
            times = counts / 50 + 1;
        }
        for (int m = 1; m <= times; m++) {
            url = pre + dateParam + after+String.valueOf(m);
            result = HttpRequest.sendGet(url, null);
            jb = JSONObject.parseObject(result);
            data = jb.getJSONObject("result").getJSONArray("data");
            for (int i = 0; i <data.size() ; i++) {
                YejiYuBao yejiYuBao = JSON.toJavaObject(data.getJSONObject(i), YejiYuBao.class);
                yejiYuBao.setCode(data.getJSONObject(i).get("SECUCODE").toString().substring(0,data.getJSONObject(i).get("SECUCODE").toString().length()-3));
                yejiYuBao.setName(data.getJSONObject(i).get("SECURITY_NAME_ABBR").toString());
                yejiYuBaos.add(yejiYuBao);
            }
        }
    }
}
